package com.example.lifecycle_demo2;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LifecycleDemo2 extends Activity {

	// class variables and constants
	public static final String MYPREFSID = "MyPrefs001";
	public static final int actMode = Activity.MODE_PRIVATE;
	LinearLayout myScreen;
	EditText txtColorSelect;
	TextView txtToDo;
	Button btnFinish;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lifecycle_demo2);
		myScreen = (LinearLayout) findViewById(R.id.myScreen);
		txtToDo = (TextView) findViewById(R.id.txtToDo); 
		txtColorSelect = (EditText)findViewById(R.id.txtColorSelect);
		String msg = "Instructions: \n ";
		txtToDo.setText(msg);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Toast.makeText(this, "onDestroy", 1).show();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		saveDataFromCurrentState();
		Toast.makeText(this, "onPause", 1).show();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Toast.makeText(this, "onRestart", 1).show();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Toast.makeText(this, "onResume", 1).show();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		updateFromSavedState();
		Toast.makeText(this, "onStart", 1).show();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Toast.makeText(this, "onStop", 1).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_lifecycle_demo2, menu);
		return true;
	}
	
	protected void saveDataFromCurrentState() {
		   SharedPreferences myPrefs = getSharedPreferences(MYPREFSID, actMode);
		   SharedPreferences.Editor myEditor = myPrefs.edit();
		   myEditor.putString("myBkColor", txtColorSelect.getText().toString());
		   myEditor.commit();
		}// saveDataFromCurrentState
	
	protected void updateFromSavedState() {
		SharedPreferences myPrefs = getSharedPreferences(MYPREFSID, actMode);
		if ((myPrefs != null) && (myPrefs.contains("myBkColor"))) {
		   String theChosenColor = myPrefs.getString("myBkColor", "");
		   txtColorSelect.setText(theChosenColor);
		     changeBackgroundColor(theChosenColor);
		}
		}// UpdateFromSavedState
	
	protected void clearMyPreferences() {
		   SharedPreferences myPrefs = getSharedPreferences(MYPREFSID, actMode);
		   SharedPreferences.Editor myEditor = myPrefs.edit();
		   myEditor.clear();
		   myEditor.commit();
		}
	
	private void changeBackgroundColor (String theChosenColor){
		// change background color
		   if (theChosenColor.contains("red"))
			myScreen.setBackgroundColor(0xff000000);
		else if (theChosenColor.contains("green"))
		   myScreen.setBackgroundColor(0xff00ff00);
		else if (theChosenColor.contains("blue"))
		   myScreen.setBackgroundColor(0xff0000ff);
		else {
			clearMyPreferences();
		//reseting user preferences
			myScreen.setBackgroundColor(0xff000000);
		}
	}
	
	/*
	protected void onRestoreInstanceState(Bundle savedInstanceState)
	This method is called after onStart() when the activity is being re-initialized from a previously saved state.
	The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).
	*/
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
	  super.onRestoreInstanceState(savedInstanceState);
	  Toast.makeText(getBaseContext(),"onRestoreInstanceState ...BUNDLING",
			  Toast.LENGTH_LONG).show();
	  
	}
	
	
	/*
	protected void onSaveInstanceState(Bundle outState)
	Called to retrieve per-instance state from an activity before being killed so that the state can be restored in
	onCreate(Bundle) or
	onRestoreInstanceState(Bundle) (the Bundle populated by this method
	will be passed to both).
	This method is called before an activity may be killed so that when it comes
	back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of
	its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via:
	onCreate(Bundle) or onRestoreInstanceState(Bundle).
	*/
	
	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{ 
		super.onSaveInstanceState(outState);
		Toast.makeText(getBaseContext(),
				"onSaveInstanceState   ...BUNDLING",
				Toast.LENGTH_LONG).show();
	}
}
